package com.zxd.admin.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.hs.common.api.dto.ResultVo;
import com.zxd.admin.dto.AdminDto;
import com.zxd.admin.entity.Admin;
import com.zxd.admin.service.IAdminService;
import com.zxd.admin.service.LoginFeignClients;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author xudongzhou
 * @date 2022 -07 -25
 * @description
 */
@RestController
@RequestMapping("/user")
@Api(tags = "用户认证")
public class LoginController {

    @Autowired
    private IAdminService adminService;

    @Qualifier("com.zxd.admin.service.LoginFeignClients")
    @Autowired
    private LoginFeignClients loginFeignClients;

    @ApiOperation("认证方法")
    @PostMapping
    public ResultVo login(@RequestBody @Validated AdminDto adminDto){
        String token = adminService.login(adminDto);

        LambdaQueryWrapper<Admin> wrapper = Wrappers.lambdaQuery(Admin.class);
        wrapper.eq(Admin::getUsername,adminDto.getUsername());
        Admin admin = adminService.getOne(wrapper);

        loginFeignClients.addLoginLog(admin.getId());

        return new ResultVo(token);
    }
}
